package com.syni.mdd.yhd.common.all.quartz.businessCoupon.job;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessCoupon;
import com.syni.mdd.yhd.common.all.service.BmsBusinessCouponService;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @className GroupMessageTimingInit
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/8 18:16
 * @VERSION 1.0
 **/
@Component
@DependsOn("applicationContextHolder")
public class CouponAsOfTimingInitialize {
    @Autowired
    BmsBusinessCouponService bmsBusinessCouponService;

    @PostConstruct
    public void init(){
        //查询所有待派发的券
        Integer pageNum = 1;
        Integer pageSize = 100;
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("status", Constants.COUPON_START);
        Set<Integer> couponType = new HashSet<>();
        couponType.add(Constants.BUSINESS_COUPON_VOUCHERS);
        couponType.add(Constants.BUSINESS_COUPON_FOOD);
        couponType.add(Constants.BUSINESS_COUPON_GROUP_BUY);
        couponType.add(Constants.BUSINESS_COUPON_CUSTOM);
        conditionMap.put("couponType_IN",couponType);
        while (true) {
            Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize);
            List<BmsBusinessCoupon> bmsBusinessCoupons = bmsBusinessCouponService.getBaseDao().findAll(new SpecificationUtils(conditionMap),pageable).getContent();
            if(bmsBusinessCoupons != null && bmsBusinessCoupons.size() > 0){
                for(BmsBusinessCoupon businessCoupon : bmsBusinessCoupons){
                    bmsBusinessCouponService.doBusinessCouponTimingByOff(CouponAsOfJob.class,businessCoupon.getSendEndTime(),businessCoupon);
                }
                pageNum++;
            }else {
                break;
            }
        }
    }
}
